<?php
/**
-------------------------
GNU GPL COPYRIGHT NOTICES
-------------------------
This file is part of FlexicaCMS.

FlexicaCMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlexicaCMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlexicaCMS.  If not, see <http://www.gnu.org/licenses/>.*/

/**
 * $Id$
 *
 * @author FlexicaCMS team <contact@flexicacms.com>
 * @link http://www.flexicacms.com/
 * @copyright Copyright &copy; 2009-2010 Gia Han Online Solutions Ltd.
 * @license http://www.flexicacms.com/license.html
 */
  
  
/**
* shortcut to create url
* 
* @param mixed $route
* @param mixed $params
* @param mixed $ampersand
*/
function url($route, $params=array ( ), $ampersand='&', $forceGetFormat = false){
    if ($forceGetFormat)
        $urlManager = new CUrlManager();
    else
        $urlManager = Yii::app()->urlManager;
    return $urlManager->createUrl($route, $params, $ampersand);
}

/**
* site base url
*/
function baseUrl() {
    return Yii::app()->request->getBaseUrl(true);
}
/**
* theme base url
*/
function themeUrl($useCdn=true){
    if(hasParam('Settings::CDN_URL')&& $useCdn==true){
        return Settings::CDN_URL;
    }
    if (Yii::app()->theme)
        return Yii::app()->theme->baseUrl;
    else
        return Yii::app()->baseUrl;
}

function workflowBaseUrl(){
    if (is_object(Yii::app()->cmsManager))
    {
        $path = parse_url('http://'.Yii::app()->cmsManager->workflow['url'], PHP_URL_PATH);
        return '/'.trim($path, '/');
    }
    else
        return '';
}

/**
* URL to site's uploads folder
*/
function uploadUrl(){
    return str_replace(DIRECTORY_SEPARATOR,'/','/sites/'.SITE_DIR.'/uploads/');
}

/**
* Application base path
*/
function basePath() {
    return Yii::app()->basePath;
}

/**
* Application runtime path
*/
function runtimePath() {
    return Yii::app()->runtimePath;
}

/**
* Application cache path
*/
function cachePath() {
    return Yii::app()->runtimePath.'/cache';
}

/**
* Application clientScript object
* @return CClientScript
*/
function cs(){
    return Yii::app()->clientScript;
}

/**
* current logged in user
* @return CWebUser
*/
function user() {
    return Yii::app()->user;
}

/**
* Check if a setting class has a defined constant
* 
* @param mixed $class
* @param mixed $param
* @return mixed parameter value or false
*/
function hasParam($setting) {
    list($class, $param) = explode('::',$setting);
    if (class_exists($class, false)) {
        $class = new ReflectionClass($class);
        return $class->getConstant($param);
    }
    return false;
}

/**
 * Send email to an address
 * 
 * @param string $email receiver's address
 * @param string $view path to view used by mailer (Yii's alias format)
 * @param array $data associative array passed to view
 */
function quickMail($email, $view, $data, $subject = 'Flexicore Member Registration Confirmation') {
    Yii::import('application.modules.Core.extensions.vendors.mail.YiiMailMessage');
    $viewName = end(explode('.',$view));
    Yii::app()->mail->viewPath = str_replace('.'.$viewName,'',$view);
    //send mail
    $message = new YiiMailMessage;
    $message->view = $viewName;
    $message->setSubject($subject);
    $message->setBody($data, 'text/html');
    $message->addTo($email);
    if (hasParam('Settings::ADMIN_EMAIL'))
        $message->setFrom(array(Settings::ADMIN_EMAIL=>Settings::SITE_NAME));
    
    try {
        Yii::app()->mail->send($message);
    }catch(Exception $ex) {
        FErrorHandler::logError($ex->getMessage());
    }
    
}
?>
